package com.gdkf.dormitory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdkf.dormitory.entity.MyPage;
import com.gdkf.dormitory.entity.Repair;
import com.gdkf.dormitory.entity.Visit;
import com.gdkf.dormitory.service.VisitService;
import com.gdkf.dormitory.tools.MyPageTool;
import com.gdkf.dormitory.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcy
 * @since 2022-09-09
 */
@RestController
public class VisitController {
    @Autowired
    private VisitService visitService;

    @PostMapping("/visit/getAllVisit")
    public Msg getAllVisit(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);

        Page<Visit> page = new Page<>(myPage.getCurrentPage(),myPage.getSizePage());
        visitService.page(page, null);
        List<Visit> records = page.getRecords();
        return  records.isEmpty()?Msg.fail():Msg.success()
                .add("visitList",page.getRecords())
                .add("sizePage",page.getSize())
                .add("currentPage",page.getCurrent())
                .add("pageTotal",page.getTotal());
    }

    @PostMapping("/visit/getOneVisit")
    public Msg getOneVisit(@RequestBody Object objectData){
        String s = objectData.toString().replaceAll("=",":");
        JSONObject templateObject = (JSONObject) JSONObject.parse(s);
//       获得第一次解析的数据
        MyPage myPage = MyPageTool.parseToPage(templateObject);
        if(myPage.getObject()!=null){
            MyPage myPage2 = MyPageTool.JsonParseToVisit(myPage);
            Page<Visit> page = new Page<>(myPage2.getCurrentPage(),myPage2.getSizePage());
            QueryWrapper<Visit> wrapper = new QueryWrapper<>();
            wrapper.eq("vi_name",((Visit)myPage2.getObject()).getViName());
            wrapper.eq("dorm_id",((Visit)myPage2.getObject()).getDormId());
            wrapper.eq("hous_id",((Visit)myPage2.getObject()).getHousId());
            Page<Visit> page1 = visitService.page(page, wrapper);
            List<Visit> records = page1.getRecords();
            return records.isEmpty()?Msg.fail():Msg.success()
                    .add("visitList",records)
                    .add("sizePage",page.getSize())
                    .add("currentPage",page.getCurrent())
                    .add("pageTotal",page.getTotal());
        }
        return Msg.fail();
    }

    @PostMapping("/visit/addVisit")
    public Msg addVisit(@RequestBody Visit visit){
        boolean save = visitService.save(visit);
        return save?Msg.success():Msg.fail();
    }

    @PostMapping("/visit/updateVisit")
    public Msg updateVisit(@RequestBody Visit visit){
        System.out.println("====updateVisit===visit====>"+visit);
        QueryWrapper<Visit> wrapper = new QueryWrapper<>();
        wrapper.eq("vi_id",visit.getViId());
        return visitService.update(visit,wrapper)?Msg.success():Msg.fail();
    }
    @PostMapping("/visit/deleteVisit")
    public Msg deleteVisit(@RequestBody Visit visit){
        QueryWrapper<Visit> wrapper = new QueryWrapper<>();
        wrapper.eq("vi_id",visit.getViId());
        wrapper.eq("dorm_id",visit.getDormId());
        wrapper.eq("hous_id",visit.getHousId());
        wrapper.eq("vi_name",visit.getViName());
        boolean remove = visitService.remove(wrapper);
        return remove?Msg.success():Msg.fail();
    }
}

